<template>
	<gracePage headerBG="#FFFFFF" :isSwitchPage="true" :customHeader="false" style="background-color: #FFFFFF;">
		<view slot="gBody" class="grace-body">
			<view class="grace-margin-top" style="text-align:right;"><text class="grace-text grace-blue" style="margin-right:25rpx;" @tap="addAddress">+ 添加新地址</text></view>
			<view class="tips">
				左滑编辑或删除地址
			</view>
			<view class="grace-margin-top"><graceSwipeList :msgs="msgs" @itemTap="itemTap" @btnTap="btnTap"></graceSwipeList></view>
		</view>
	</gracePage>
</template>
<script>
const app=getApp();
import{ myAddress,delMyAddress } from '@/api/index.js'
import gracePage from '../../graceUI/components/gracePage.vue';
import graceSwipeList from '../../graceUI/components/graceSwipeList.vue';
export default {
	data() {
		return {
			msgs: [],
			openId:"",
		};
	},
	onLoad() {

	},
	onShow(){
		this.openId=uni.getStorageSync('openid');
		this.myAddress();
	},
	methods: {
		// 初始化收获地址
		myAddress:function(){
			myAddress({openid:this.openId}).then(res=>{
				res.forEach((obj,i)=>{
					obj.content=`${obj.pro} ${obj.city} ${obj.area} ${obj.address}  ${obj.phone}`
					// obj.pro + '-' obj.city + '-' + obj.area + ' ' + '手机号:' + obj.phone;
					obj.btns=[{ name: '编辑', bgColor: '#323232' }, { name: '删除', bgColor: '#FF0036' }];
				})
				this.msgs=res
			})
		},
		
		btnTap: function(index, btnIndex) {
			let id=this.msgs[index].id
			// 编辑地址
			if (btnIndex == 0) {
				uni.navigateTo({
					url:`/pageA/add-address/add-address?id=${id}`
				})
			}
			// 删除地址
			else if (btnIndex == 1) {
				uni.showModal({
					title: '确定要删除吗?',
					success: e => {
						if (e.confirm) {
							delMyAddress({
								id:id,
								openid:this.openId
							}).then(res=>{
								if(res==1){
									uni.showToast({
										title: '删除成功!',
										duration: 1000
									})
									this.msgs.splice(index, 1);
								}
							})
						}
					}
				});
			}
		},
		// 列表本身被点击
		itemTap: function(e) {
			let pages = getCurrentPages();
			if(pages[0].data.pages!=""){
				app.globalData.address=this.msgs[e];
				uni.navigateBack({
					delta:1
				})
			}
		},
		
		// 添加地址
		addAddress: function() {
			uni.navigateTo({
				url:'/pageA/add-address/add-address'
			})
		}
	},
	components: { gracePage, graceSwipeList }
};
</script>
<style lang="scss" scoped>
	.tips{
		font-size: 24upx;
		padding-left: 20upx;
		color: #999;
	}
</style>
